<?php
/**
 * 在线支付交易记录专属类，请勿污染
 * User: lanlc
 * Date: 17-2-28
 * Time: 上午10:36
 */

namespace app\backend\model;

use think\Model;

class JournalOnline extends Model
{
    public function __construct() {
        parent::__construct();
    }

    private $_table      = 'z_alipay';  //类对象对应的数据库表
    private $_orderNum   = '';  //订单号
    private $_orderId    = '';  //订单id数组
    private $_serialNum  = '';  //交易流水
    private $_sellerEmail= '';  //收款方
    private $_buyerEmail = '';  //付款方
    private $_subject    = '';  //描述
    private $_money      = '';  //交易金额
    private $_tradeNumber= '';  //交易单号
    private $_time       = '';  //时间
    private $_sourceT    = '';  //来源
    private $_status     = '';  //状态
    private $_type       = '';  //账单类型
    private $_beginCycle = '';  //起始周期
    private $_endCycle   = '';  //结束周期

    /**
     * 设置全部属性方法
     * @date   2017-02-28
     * @author Lanlc
     *
     * @return
     */
    public function setAllProperty($orderNum, $orderId, $serialNumber, $sellerEmail, $buyerEmail, $subject, $money, $tradeNumber, $time, $sourceT, $status, $type, $beginCycle, $endCycle)
    {
        $this->_orderNum     = $orderNum;
        $this->_orderId      = $orderId;
        $this->_serialNum    = $serialNumber;
        $this->_sellerEmail  = $sellerEmail;
        $this->_buyerEmail   = $buyerEmail;
        $this->_subject      = $subject;
        $this->_money        = $money;
        $this->_tradeNumber  = $tradeNumber;
        $this->_time         = $time;
        $this->_sourceT      = $sourceT;
        $this->_status       = $status;
        $this->_type         = $type;
        $this->_beginCycle   = $beginCycle;
        $this->_endCycle     = $endCycle;
    }
    /**
     * 判断是否是对象属性
     * @date   2017-02-28
     * @author Lanlc
     *
     * @param  string  $param  待判断的对象属性
     *
     * @return
     */
    public function _isset($param)
    {
        if (property_exists($this, $param)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 设置对象属性
     * @date   2017-02-28
     * @author Lanlc
     *
     * @param  string  $name   属性名称
     * @param  mix     $value  属性值
     *
     * @return
     */
    public function setValue($name, $value)
    {
        if ($this->_isset($name)) {
            $this->$name = $value;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取对象属性
     * @date   2017-02-28
     * @author Lanlc
     *
     * @param  string  $name   属性名称
     *
     * @return
     */
    public function getValue($name)
    {
        if ($this->_isset($name)) {
            return $this->$name;
        } else {
            return false;
        }
    }

    /**
     * 添加在线支付交易记录
     * @date   2017-02-28
     * @author Lanlc
     *
     * @param
     *
     * @return
     */
    public function addRecord()
    {
        $data = [
            'ordernum'     => $this->_orderNum,
            'order_id'     => $this->_orderId,
            'serial_number'=> $this->_serialNum,
            'seller_email' => $this->_sellerEmail,
            'buyer_email'  => $this->_buyerEmail,
            'subject'      => $this->_subject,
            'money'        => $this->_money,
            'trade_no'     => $this->_tradeNumber,
            'time'         => $this->_time,
            'sourceT'      => $this->_sourceT,
            'status'       => $this->_status,
            'type'         => $this->_type,
            'begin_cycle'  => $this->_beginCycle,
            'end_cycle'    => $this->_endCycle,
        ];
        $addResult = $this->setTable($this->_table)->insertGetId($data);

        return $addResult ? $addResult : false;
    }

    /**
     * 更新在线支付记录
     * @date   2017-0228
     * @author Lanlc
     *
     *
     * @return
     */
    public function updateRecord()
    {
        $data = [
            'trade_no'     => $this->_tradeNumber,
            'time'         => $this->_time,
            'status'       => $this->_status,
        ];

        $where = [
            'serial_number' => $this->_serialNum,
            'begin_cycle'   => $this->_beginCycle,
            'end_cycle'     => $this->_endCycle,
            'type'          => $this->_type,
        ];
        $setResult =  $this->setTable($this->_table)->where($where)->update($data);
        return $setResult ? $setResult : false;
    }

    /**
     * 检测是否已经存在记录
     * @date   2017-03-23
     * @author Lanlc
     *
     * @return bool
     */
    public function chkOnlineRec() {
        $where = [
            'ordernum'    => $this->_orderNum,
            'order_id'    => $this->_orderId,
            'begin_cycle' => $this->_beginCycle,
            'end_cycle'   => $this->_endCycle,
            'type'        => $this->_type,
            'sourceT'     => $this->_sourceT,
            'status'      => 0,
        ];
        $res =  $this->setTable($this->_table)->field('id, serial_number')->where($where)->find();
        return $res ? $res : false;
    }

    /**
     * 根据交易流水号获取这条记录信息
     * @date   2017-05-24
     * @author wuhao
     *
     * @return array
     */
    public function getRecordBySerial()
    {
        $res =  $this->setTable($this->_table)->field('*')->where(['serial_number' => $this->_serialNum])->find();
        return $res ?: [];
    }

    /**
     * 删除这条记录
     * @date   2017-05-24
     * @author wuhao
     *
     * @param int  $id  id
     * @return array
     */
    public function deleteById($id)
    {
        if (!$id) {
            return false;
        }
        $where = ['id' => $id];
        return $this->setTable($this->_table)->where($where)->delete();
    }

    /**
     * @comment  获取在线交易记录
     * @author wuhao
     * @param array $where
     * @param string $field
     */
    public function getList(array $where, $field = '*')
    {
        return $this->table($this->_table)->field($field)->where($where)->select();
    }
}
